<?php
header("Content-type: text/html; charset=utf-8"); 

include('../wp-config.php');

$pwd = '$Needpublishpassw0rD;';
if ($_GET['p'] != $pwd) exit;
if ($_POST['submit'] == 'go') {
  if ($_POST['data'] != '') {
    _insert_data($_POST['data']);
	echo '批量导入分类成功!';
	exit;
  }
}

?>
<form method="post">
输入分类字符串:
<br />
<textarea name="data" value="" style="width:545px;height:250px;"></textarea>
<input type="submit" name="submit" value="go" />
</form>
<?php
function _insert_data($data) {
  global $table_prefix;
  $sql = '';
  $arr = split('\|\|\|', trim($data));
  _connect_db();
  //print_r( mysql_db_query(DB_NAME, 'INSERT INTO wp_users * FROM wp_users') );
  //exit;
  foreach ($arr as $val) {
    $sql = 'INSERT INTO `' . $table_prefix . 'terms` ' . 
	  '(`name`, `slug`, `term_group`) VALUES ("' . $val . '", "' . _replace($val) . '", 0)';
	//echo $sql; exit;
	_execute_sql($sql);
	$new_cat_id = mysql_insert_id();
    $sql = 'INSERT INTO `' . $table_prefix . 'term_taxonomy` ' . 
	  '(`term_id`, `taxonomy`, `description`, `parent`, `count`) ' .
      'VALUES (' . $new_cat_id . ', "category", "", "0", "0")';
    _execute_sql($sql);
  }
  mysql_close();
}

function _execute_sql($sql) {  
  mysql_db_query(DB_NAME, $sql);
}

function _connect_db() {
  return mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
}

function _replace($str) {
  $result = '';
  $str = html_entity_decode($str, ENT_QUOTES);
  $words = str_word_count($str, 1);  
  foreach ($words as $val) {
    $val = preg_replace('/[\'\&]/', '', $val);
    $result = ($result == '') ? $val : $result . '-' . $val;
  }
  $result = strtolower($result);
  return $result;
}
?>